// CtrlPanelDlg.cpp : implementation file
//

#include "stdafx.h"
#include "resource.h"
#include "CtrlPanelDlg.h"
#include "BtnST.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CCtrlPanelDlg dialog

#define TIMERID_SHOW_CRT_POS	1000

CCtrlPanelDlg::CCtrlPanelDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCtrlPanelDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCtrlPanelDlg)
	m_lfXYGotoX = 0.0;
	m_lfXYGotoY = 0.0;
	m_lfXYStepX = 0.0;
	m_lfXYStepY = 0.0;
	m_lfSingMoveTo = 0.0;
	m_lfSingStepMove = 0.0;
	m_nSingMoveTo = 0;
	m_nSingStepMove = 0;
	//}}AFX_DATA_INIT
}


void CCtrlPanelDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCtrlPanelDlg)
	DDX_Control(pDX, IDC_BUTTON_STOP_ALL, m_ctrStopAll);
	DDX_Control(pDX, IDC_STATIC_Z, m_ctrZCrtPos);
	DDX_Control(pDX, IDC_STATIC_Y, m_ctrYCrtPos);
	DDX_Control(pDX, IDC_STATIC_X, m_ctrXCrtPos);
	DDX_Control(pDX, IDC_BUTTON_ANTI_CLOCKWISE, m_ctrAntiClockwise);
	DDX_Control(pDX, IDC_BUTTON_CLOCKWISE, m_ctrClockwise);
	DDX_Control(pDX, IDC_BUTTON_DOWN, m_ctrDown);
	DDX_Control(pDX, IDC_BUTTON_LEFT, m_ctrLeft);
	DDX_Control(pDX, IDC_BUTTON_RIGHT, m_ctrRight);
	DDX_Control(pDX, IDC_BUTTON_UP, m_ctrlUp);
	DDX_Control(pDX, IDC_BUTTON_FIND_HOME, m_ctrFindHome);
	DDX_Text(pDX, IDC_EDIT_XY_GOTO_X, m_lfXYGotoX);
	DDX_Text(pDX, IDC_EDIT_XY_GOTO_Y, m_lfXYGotoY);
	DDX_Text(pDX, IDC_EDIT_XY_STEP_MOVE_X, m_lfXYStepX);
	DDX_Text(pDX, IDC_EDIT_XY_STEP_MOVE_Y, m_lfXYStepY);
	DDX_Text(pDX, IDC_EDIT_SING_MOVE_TO, m_lfSingMoveTo);
	DDX_Text(pDX, IDC_EDIT_SING_STEP_MOVE, m_lfSingStepMove);
	DDX_CBIndex(pDX, IDC_COMBO_SING_MOVE_TO, m_nSingMoveTo);
	DDX_CBIndex(pDX, IDC_COMBO_SING_STEP_MOVE, m_nSingStepMove);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CCtrlPanelDlg, CDialog)
	//{{AFX_MSG_MAP(CCtrlPanelDlg)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BUTTON_FIND_HOME, OnButtonFindHome)
	ON_BN_CLICKED(IDC_BUTTON_XY_GOTO, OnButtonXyGoto)
	ON_BN_CLICKED(IDC_BUTTON_SING_MOVETO, OnButtonSingMoveto)
	ON_BN_CLICKED(IDC_BUTTON_XY_STEP_MOVE, OnButtonXyStepMove)
	ON_BN_CLICKED(IDC_BUTTON_SING_STEP_MOVE, OnButtonSingStepMove)
	ON_BN_CLICKED(IDC_BUTTON_STOP_ALL, OnButtonStopAll)
	ON_WM_NCHITTEST()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCtrlPanelDlg message handlers

BOOL CCtrlPanelDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	m_ctrAntiClockwise.SetIcon(IDI_ICON_ANTI_CLOCKWISE);
	m_ctrClockwise.SetIcon(IDI_ICON_CLOCKWISE);
	m_ctrDown.SetIcon(IDI_ICON_DOWN_WARD);
	m_ctrLeft.SetIcon(IDI_ICON_LEFT_WARD);
	m_ctrRight.SetIcon(IDI_ICON_RIGHT_WARD);
	m_ctrlUp.SetIcon(IDI_ICON_UP_WARD);
	m_ctrFindHome.SetIcon(IDI_ICON_HOME);
	m_ctrStopAll.SetIcon(IDI_ICON_STOP);

	SetTimer(TIMERID_SHOW_CRT_POS, 100, NULL);
	
	return TRUE;  
}

BOOL CCtrlPanelDlg::PreTranslateMessage(MSG* pMsg) 
{
	if (pMsg->message!=WM_LBUTTONDOWN && pMsg->message!=WM_LBUTTONUP)
	{
		goto LClearup;
	}

	
	if (pMsg->hwnd == m_ctrlUp.m_hWnd)
	{
		switch (pMsg->message)
		{
		case WM_LBUTTONDOWN:
			m_pMotionControlCard->JogMove(AXIS_Y, MOVE_DIR_FORWARD);
			break;

		case WM_LBUTTONUP:
			m_pMotionControlCard->StopMove(AXIS_Y);
			break;
		}
	}
	else if (pMsg->hwnd == m_ctrDown.m_hWnd)
	{
		switch (pMsg->message)
		{
		case WM_LBUTTONDOWN:
			m_pMotionControlCard->JogMove(AXIS_Y, MOVE_DIR_BACKWARD);
			break;
			
		case WM_LBUTTONUP:
			m_pMotionControlCard->StopMove(AXIS_Y);
			break;
		}
	}
	else if (pMsg->hwnd == m_ctrLeft.m_hWnd)
	{
		switch (pMsg->message)
		{
		case WM_LBUTTONDOWN:
			m_pMotionControlCard->JogMove(AXIS_X, MOVE_DIR_BACKWARD);
			break;
			
		case WM_LBUTTONUP:
			m_pMotionControlCard->StopMove(AXIS_X);
			break;
		}
	} 
	else if (pMsg->hwnd == m_ctrRight.m_hWnd)
	{
		switch (pMsg->message)
		{
		case WM_LBUTTONDOWN:
			m_pMotionControlCard->JogMove(AXIS_X, MOVE_DIR_FORWARD);
			break;
			
		case WM_LBUTTONUP:
			m_pMotionControlCard->StopMove(AXIS_X);
			break;
		}
	} 
	else if (pMsg->hwnd == m_ctrClockwise.m_hWnd)
	{
		switch (pMsg->message)
		{
		case WM_LBUTTONDOWN:
			m_pMotionControlCard->JogMove(AXIS_Z, MOVE_DIR_FORWARD);
			break;
			
		case WM_LBUTTONUP:
			m_pMotionControlCard->StopMove(AXIS_Z);
			break;
		}
	} 
	else if (pMsg->hwnd == m_ctrAntiClockwise.m_hWnd)
	{
		switch (pMsg->message)
		{
		case WM_LBUTTONDOWN:
			m_pMotionControlCard->JogMove(AXIS_Z, MOVE_DIR_BACKWARD);
			break;
			
		case WM_LBUTTONUP:
			m_pMotionControlCard->StopMove(AXIS_Z);
			break;
		}
	}


LClearup:
	return CDialog::PreTranslateMessage(pMsg);
}

void CCtrlPanelDlg::OnTimer(UINT nIDEvent) 
{
	if (TIMERID_SHOW_CRT_POS==nIDEvent)
	{
		CString strTmp;
		strTmp.Format(_T("%.3lf"), m_pMotionControlCard->GetCrtPos(AXIS_X));
		m_ctrXCrtPos.SetText(strTmp);
		
		strTmp.Format(_T("%.3lf"), m_pMotionControlCard->GetCrtPos(AXIS_Y));
		m_ctrYCrtPos.SetText(strTmp);

		strTmp.Format(_T("%.3lf"), m_pMotionControlCard->GetCrtPos(AXIS_Z));
		m_ctrZCrtPos.SetText(strTmp);
	}
		
	CDialog::OnTimer(nIDEvent);
}

void CCtrlPanelDlg::OnButtonFindHome() 
{
	m_pMotionControlCard->FindHome_XY();	
}

void CCtrlPanelDlg::OnButtonStopAll() 
{
	m_pMotionControlCard->StopMove(AXIS_X);
	m_pMotionControlCard->StopMove(AXIS_Y);
	m_pMotionControlCard->StopMove(AXIS_Z);
}

void CCtrlPanelDlg::OnButtonXyGoto() 
{
	UpdateData();
	m_pMotionControlCard->MoveTo_XY(m_lfXYGotoX, m_lfXYGotoY);
}

void CCtrlPanelDlg::OnButtonSingMoveto() 
{
	UpdateData();
	m_pMotionControlCard->MoveTo((AXIS_NUM)m_nSingMoveTo, m_lfSingMoveTo);
}

void CCtrlPanelDlg::OnButtonXyStepMove() 
{
	UpdateData();
	m_pMotionControlCard->IncMove_XY(m_lfXYStepX, m_lfXYStepY);
}

void CCtrlPanelDlg::OnButtonSingStepMove() 
{
	UpdateData();
	m_pMotionControlCard->IncMove((AXIS_NUM)m_nSingStepMove, m_lfSingStepMove);
}

UINT CCtrlPanelDlg::OnNcHitTest(CPoint point) 
{
	UINT nHitTest = CDialog::OnNcHitTest(point);
	return (nHitTest==HTCLIENT) ? HTCAPTION : nHitTest;
}

void CCtrlPanelDlg::OnCancel() 
{
	
	CDialog::OnCancel();
}

void CCtrlPanelDlg::OnOK() 
{
	CDialog::OnOK();
}
